package com.sky.controller.admin;


import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param dto
     * @return
     */

    //@CacheEvict清理指定套餐id下的换成
    @CacheEvict(cacheNames = "setmeal", key = "#dto.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping()
    public Result insert(@RequestBody SetmealDTO dto){
        log.info("新增套餐：{}", dto);
        setmealService.insert(dto);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("page")
    public Result page(SetmealPageQueryDTO dto){
        log.info("分页查询套餐列表：{}", dto);
        PageResult pageResult = setmealService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("查询id为{}的套餐", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping()
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐：{}", dto);
        setmealService.update(dto);
        return Result.success();
    }

    /**
     * 起售停售状态修改
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("起售停售状态修改")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("修改套餐{}状态为{}...",id, status);
        setmealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除套餐：{}", ids);
        setmealService.delete(ids);
        return Result.success();
    }
}

